/**************************************************************************
File: hellomouse.c
Does: basic drawing and interaction in OpenGL/GLUT
Author: gem, based on hwa's
Date: 01/08/08
**************************************************************************/
/**********
notes:
VS users: include opengl/glut.h below instead of GL/glut.h
OSX users: include glut/glut.h below instead of GL/glut.h
**********/

#include "MCircle.h"
#include "MRect.h"
#include <GL\glut.h>
#include "hellomouse.h"
#include <stdio.h>
#include "SceneManager.h"
using namespace std;

/* define and initialize some global variables */
static int window_w = 400, window_h = 400; /* window height and width */
static const int REFRESH_TIME = 20;
static SceneManager myScene(400,400);

/*Typical OpenGL/GLUT Main function */ 
int main(int argc, char **argv) { /* program arguments */

	/* initialize GLUT and OpenGL; Must be called first */
	glutInit( &argc, argv ) ;

	/* our own initializations; we'll define these setup procedures */
	glut_setup() ;  
	gl_setup() ;
	my_setup();

	/* turn control over to GLUT */
	glutMainLoop() ;

	return(0) ; /* make the compiler happy */
}



/* Typical function to set up GLUT*/
void glut_setup(void) {

	/* specify display mode -- here we ask for double buffering and RGB coloring */
	glutInitDisplayMode (GLUT_DOUBLE |GLUT_RGB);

	/* make a window of size window_w by window_h; title of "GLUT Basic Interaction" placed at top left corner */
	glutInitWindowSize(window_w, window_h);
	glutInitWindowPosition(0,0);
	glutCreateWindow("GLUT Basic Interaction");

	/*initialize typical callback functions */
	glutDisplayFunc( my_display );
	glutReshapeFunc( my_reshape ); 
	glutIdleFunc( my_idle );
	glutMouseFunc( my_mouse );	
	glutKeyboardFunc( my_keyboard );	
	glutTimerFunc( REFRESH_TIME, my_TimeOut, 0);/* schedule a my_TimeOut call with the ID 0 in 20 seconds from now */
	return ;
}

/* Typical function to set up OpenGL */
/* For now, ignore the contents of function */
void gl_setup(void) {

	/* specifies a background color: black in this case */
	glClearColor(0,0,0,0) ;

	/* setup for simple 2d projection */
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity();
	/* map unit square to viewport window */
	gluOrtho2D(0.0, 1.0, 0.0, 1.0); 

	return ;
}


void my_setup(void) {
	return;
}

/* drawing function */
/* TODO: draw some other shape, use some other colors */
/* TODO part 2: draw at the location of the mouse click */
void my_display(void) {
	myScene.my_display();
}

/* called if resize event (i.e., when we create the window and later whenever we resize the window) */ 
void my_reshape(int w, int h) {
	myScene.my_reshape(w,h);
}

/* called if mouse event (i.e., mouse click) */
/* TODO: correct my_y so we draw exactly where the mouse clicked */
/* TODO for EC: have more fun in here */
void my_mouse(int b, int s, int x, int y) {
	myScene.my_mouse(b,s,x,y);
}


/* called if keyboard event (keyboard key hit) */
/* TODO for EC: do something fun in here */
void my_keyboard(unsigned char c, int x, int y) {
	myScene.my_keyboard(c,x,y);
}


/* called if timer event */
/* TODO for EC: do something fun in here */
void my_TimeOut(int id) { 
	switch (id) {
	case 0:	//event id 0
		myScene.my_timeout(id);
		glutTimerFunc(REFRESH_TIME, my_TimeOut, 0);/* schedule next timer event, 1 secs from now */
		break;
	}

}

void my_idle() {

}